package com.iot.service.demo;

import io.netty.util.HashedWheelTimer;
import io.netty.util.concurrent.DefaultThreadFactory;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author: hszhou
 * @Date: 2025/5/7 14:02
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Slf4j
public class HashWheelTimerDemo {

    public static void main(String[] args) throws InterruptedException {


        HashedWheelTimer timer = new HashedWheelTimer(new DefaultThreadFactory("tl"),100, TimeUnit.MILLISECONDS,  512);

        log.info("[{}]===",timer);

        List<AppointmentRecord> appointment = getAppointment();
//        appointment
        CustomTask customTask = new CustomTask(new AppointmentRecord("1", null, null));
        CustomTask customTask2 = new CustomTask(new AppointmentRecord("2", null, null));
         timer.newTimeout(customTask, 3, TimeUnit.SECONDS);
       timer.newTimeout(customTask2, 3, TimeUnit.SECONDS);
        Thread.sleep(30000);

        log.info("取消任务");
        customTask.getTimeout().cancel();
        //timeout.cancel();

    }

    public static List<AppointmentRecord> getAppointment(){
        List<AppointmentRecord> records = new ArrayList<>();
        AppointmentRecord appointmentRecord1 = new AppointmentRecord("1", LocalDateTime.now(), LocalDateTime.now().plusMinutes(1));
        AppointmentRecord appointmentRecord2 = new AppointmentRecord("2", LocalDateTime.now(), LocalDateTime.now().plusMinutes(2));
        AppointmentRecord appointmentRecord3 = new AppointmentRecord("3", LocalDateTime.now(), LocalDateTime.now().plusMinutes(3));
        AppointmentRecord appointmentRecord4 = new AppointmentRecord("4", LocalDateTime.now(), LocalDateTime.now().plusMinutes(4));
        records.add(appointmentRecord1);
        records.add(appointmentRecord2);
        records.add(appointmentRecord3);
        records.add(appointmentRecord4);
        return records;
    }
}
